<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="车牌号" prop="carNo">
                <el-input v-model="queryParams.carNo" placeholder="请输入车牌号" clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="车型号" prop="carModel">
                <el-input v-model="queryParams.carModel" placeholder="请输入车型号" clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="目的地" prop="destination" v-if="billType=='car_apply'">
                <el-input v-model="queryParams.destination" placeholder="请输入目的地" clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="流程状态" prop="status">
                <el-select v-model="queryParams.status" placeholder="请选择流程状态" clearable>
                    <el-option v-for="dict in dict.type.oa_flow_billstatus" :key="dict.value" :label="dict.label" :value="dict.value"/>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['oa:carApply:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['oa:carApply:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table :border="Global.tableShowBorder" v-loading="loading" :data="carApplyList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="单据编号" align="center" prop="billCode"/>
            <el-table-column label="车牌号" align="center" prop="carNo"/>
            <el-table-column label="车型号" align="center" prop="carModel"/>
            <el-table-column label="使用缘由" align="center" prop="reason" :show-overflow-tooltip="true" v-if="billType=='car_apply'"/>
            <el-table-column label="目的地" align="center" prop="destination" :show-overflow-tooltip="true" v-if="billType=='car_apply'"/>

            <el-table-column label="维修地址" align="center" prop="repairAddress" :show-overflow-tooltip="true" v-if="billType=='car_repair'"/>
            <el-table-column label="维修内容" align="center" prop="repairContent" :show-overflow-tooltip="true" v-if="billType=='car_repair'"/>
            <el-table-column label="花费金额" align="center" prop="costAmt" :show-overflow-tooltip="true" v-if="billType=='car_maintenance' || billType=='car_repair'"/>
            <el-table-column label="保养地址" align="center" prop="maintenanceAddress" :show-overflow-tooltip="true" v-if="billType=='car_maintenance'"/>
            <el-table-column label="保养内容" align="center" prop="maintenanceContent" :show-overflow-tooltip="true" v-if="billType=='car_maintenance'"/>
            <el-table-column label="报废类型" align="center" prop="scrapType" :show-overflow-tooltip="true" v-if="billType=='car_scrap'"/>
            <el-table-column label="收入金额" align="center" prop="incomeAmt" :show-overflow-tooltip="true" v-if="billType=='car_scrap'"/>

            <el-table-column label="联系电话" align="center" prop="contactsPhone"/>
            <el-table-column label="流程状态" align="center" prop="status">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.oa_flow_billstatus" :value="scope.row.status"/>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="250">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" v-show="scope.row.status=='0' || scope.row.status=='3'"  icon="el-icon-check" @click="doSubmitFlow(scope.row)" v-hasPermi="['oa:carApply:edit']">提交审批</el-button>
                    <el-button size="mini" type="text" v-show="scope.row.status=='0' || scope.row.status=='3'"  icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['oa:carApply:edit']">修改</el-button>
                    <el-button size="mini" type="text" v-show="scope.row.status=='0' || scope.row.status=='3'"  icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['oa:carApply:remove']">删除</el-button>

                    <el-button size="mini" type="text" v-show="scope.row.status=='1'"  icon="el-icon-refresh-right" @click="doCancleFlow(scope.row)" v-hasPermi="['oa:carApply:edit']" style="color: #ff6666">撤销申请</el-button>
                    <el-button size="mini" type="text" v-show="scope.row.status!='0'"  icon="el-icon-rank"          @click="flowDetail(scope.row)" v-hasPermi="['oa:carApply:edit']">流程信息</el-button>
                    <el-button size="mini" type="text" v-show="scope.row.status!='0'"  icon="el-icon-view"          @click="handleView(scope.row)" v-hasPermi="['oa:carApply:edit']">查看</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

        <FlowInfo :businessId="businessId" :billType="billType" v-if="showFlowInfo" :showStatus="showFlowInfo" @closeFlowInfoDialog="closeFlowInfoDialog"></FlowInfo>

    </div>
</template>

<script>
import {listCarApply, getCarApply, delCarApply, addCarApply, updateCarApply} from "@/api/oa/carApply";
import {submitFlow,cancleFlow} from "@/api/workflow/processInstance";
import FlowInfo from "@/views/workflow/flow/flowInfo";
export default {
    name: "CarApply",
    dicts: ['oa_flow_billstatus'],
    components:{FlowInfo},
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 车辆申请表格数据
            carApplyList: [],
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 20,
                carId: null,
                carNo: null,
                carModel: null,
                reason: null,
                destination: null,
                userId: null,
                deptId: null,
                billType:"",
                orderByColumn:"createTime",
                isAsc:"descending",
            },
            billType:"",
            showFlowInfo:false,
            businessId:null
        };
    },
    created() {
        const billType = this.$route.query && this.$route.query.billType;
        console.log("单据类型："+billType)
        if(billType){
            this.queryParams.billType = billType;
            this.billType = billType;
        }
        this.getList();
    },
    methods: {
        /** 查询车辆申请列表 */
        getList() {
            this.loading = true;
            listCarApply(this.queryParams).then(response => {
                this.carApplyList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.$router.push({path: "/oa/car/carApply/form/add/"+this.billType})
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            const id = row.id
            this.$router.push({path: "/oa/car/carApply/form/" + row.id});
        },
        /** 查看单据详情 */
        handleView(row){
            this.$router.push({path: "/oa/car/carApply/form/" + row.id, query: {readonly:"1"}})
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除车辆申请编号为"' + ids + '"的数据项？').then(function () {
                return delCarApply(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => {
            });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('oa/carApply/export', {
                ...this.queryParams
            }, `carApply_${new Date().getTime()}.xlsx`)
        },



        /** 提交审批 ***/
        doSubmitFlow(row){
            let data = {
                businessId:row.id,
                billType: row.billType
            }
            this.$modal.confirm('确认要提交该单据进行审批吗？').then(function () {
                return submitFlow(data);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("提交成功");
            }).catch(() => {
            });
        },
        /** 取回审批 ***/
        doCancleFlow(row){
            let data = {
                businessId:row.id,
                billType: row.billType
            }
            this.$modal.confirm('确认要取回该协议审批吗？').then( ()=> {
                cancleFlow(data).then(res=>{
                    if(200==res.code){
                        this.getList();
                        this.$modal.msgSuccess("取回成功");
                    }
                });
            })
        },
        /** 流程信息 ***/
        flowDetail(row){
            this.businessId = row.id+"";
            this.showFlowInfo = true;
        },
        //关闭流程信息窗口
        closeFlowInfoDialog(){
            this.showFlowInfo = false;
        },
    }
};
</script>
